﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Uber.HabboHotel.GameClients;
using System.Data;
using Uber.Storage;
using Uber.Collections;
using System.Text.RegularExpressions;

namespace Uber.HabboHotel.Misc
{
    class ChatWordFilter
    {
        public SafeList<string> Words;

        public ChatWordFilter()
        {
            this.Words = new SafeList<string>();
        }

        public void InitWords()
        {
            this.Words = null;
            this.Words = new SafeList<string>();

            DataTable Data = null;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                Data = dbClient.ReadDataTable("SELECT word FROM wordfilter");
            }

            if (Data == null)
            {
                return;
            }

            foreach (DataRow Row in Data.Rows)
            {
                this.Words.Add((string)Row["word"]);
            }
        }

        public String CheckWords(string Message, GameClient Session)
        {
            for (int i = 0; i < this.Words.Count; i++)
            {
                string Word = this.Words[i];

                if (Word.Length < 0 || Word == "" || Word == null)
                {
                    continue;
                }

                if (Message.ToLower().Contains(Word.ToLower()))
                {
                    Session.GetHabbo().HandleInfraction(Message);

                    Message = Message.Replace(Word, "*****");
                }
            }

            return Message;
        }
    }
}
